package com.qf.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsKeyword;
import com.qf.service.KeywordService;
import com.qf.util.ResponseUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @version 1.0
 * @auther Administrator
 * @date 2023/2/24 22:19
 */
@RestController
@RequestMapping("/admin/keyword")
@CrossOrigin
public class KeywordController {

    @Autowired
    private KeywordService keywordService;

    /**
     * 关键词显示
     * @param page
     * @param limit
     * @param sort
     * @param order
     * @param keyword
     * @param url
     * @return
     */
    @RequiresPermissionsDesc(menu = {"商场管理","关键词"},button = "查询")
    @RequiresPermissions(value = "admin:keyword:list")
    @GetMapping("/list")
    public Object list(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer limit,
                       String sort,String order,String keyword, String url){
        IPage<DtsKeyword> iPage = keywordService.findListByQuery(page,limit,sort,order,keyword,url);
        Map data = new HashMap<>();
        data.put("total",iPage.getTotal());
        data.put("items",iPage.getRecords());
        return ResponseUtil.ok(data);
    }

    /**
     * /create
     */
    @RequiresPermissions(value = "admin:keyword:create")
    @RequiresPermissionsDesc(menu = {"商场管理","关键词"},button = "添加")
    @PostMapping("/create")
    public Object create(@RequestBody DtsKeyword dtsKeyword){
        dtsKeyword = keywordService.create(dtsKeyword);
        return ResponseUtil.ok(dtsKeyword);
    }

    /**
     * update
     */
    @RequiresPermissions(value = "admin:keyword:update")
    @RequiresPermissionsDesc(menu = {"商场管理","商场管理"},button = "编辑")
    @PostMapping("/update")
    public Object update(@RequestBody DtsKeyword dtsKeyword){
        dtsKeyword = keywordService.update(dtsKeyword);
        return ResponseUtil.ok(dtsKeyword);
    }

    /**
     * delete
     */
    @RequiresPermissions(value = "admin:keyword:delete")
    @RequiresPermissionsDesc(menu = {"商场管理","商场管理"},button = "删除")
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsKeyword dtsKeyword){
        dtsKeyword = keywordService.delete(dtsKeyword);
        return ResponseUtil.ok(dtsKeyword);
    }

}
